package com.android.camera.ui;

import com.android.camera.Log;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class RGK_ControlPanelLayout extends ViewGroup {
    private int mCellWidth;
    private int mCellHeight;

    private final String TAG = "RGK_ControlPanelLayout";

    public RGK_ControlPanelLayout(Context context) {
        super(context);
    }

    public RGK_ControlPanelLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RGK_ControlPanelLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setmCellWidth(int w) {
        mCellWidth = w;
        requestLayout();
    }

    public void setmCellHeight(int h) {
        mCellHeight = h;
        requestLayout();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int cellWidth = mCellWidth;
        int cellHeight = mCellHeight;
        int columns = (r - l) / cellWidth;
        if (columns < 0) {
            columns = 1;
        }
        int x = 0;
        int y = 0;
        int i = 0;
        int count = getChildCount();
        Log.d(TAG, "onLayout count:" + count);
        for (int j = 0; j < count; j++) {
            final View childView = getChildAt(j);

            int w = childView.getMeasuredWidth();
            int h = childView.getMeasuredHeight();

            if (childView.getVisibility() != View.GONE) {
                Log.d(TAG, "onLayout childView j:" + j + ", w:" + w + ", h:" + h);
                int left = x + ((cellWidth - w) / 2);
                int top = y + ((cellHeight - h) / 2);
                childView.layout(left, top, left + w, top + h);

                if (i >= (columns - 1)) {
                    i = 0;
                    x = 0;
                    y += cellHeight;
                } else {
                    i++;
                    x += cellWidth;
                }
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth,
                MeasureSpec.AT_MOST);
        int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight,
                MeasureSpec.AT_MOST);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            /*
             * 090 This is called to find out how big a view should be. 091 The
             * parent supplies constraint information in the width and height
             * parameters. 092 The actual mesurement work of a view is performed
             * in onMeasure(int, int), 093 called by this method. 094 Therefore,
             * only onMeasure(int, int) can and must be overriden by subclasses.
             * 095
             */
            childView.measure(cellWidthSpec, cellHeightSpec);
        }
        setMeasuredDimension(resolveSize(mCellWidth * count, widthMeasureSpec),
                resolveSize(mCellHeight * count, heightMeasureSpec));
        // setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);

        // super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}
